<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\store\controller;

use app\store\service\CompanyProfileFoodService;
use controller\BasicAdmin;
use service\DataService;
use service\ToolsService;
use think\Db;
use think\exception\HttpResponseException;

/**
 * 商店商品管理
 * Class Goods
 * @package app\store\controller
 * @author Anyon <zoujingli@qq.com>
 * @date 2017/03/27 14:43
 */
class CompanyProfileFood extends BasicAdmin
{

    /**
     * 定义当前操作表名
     * @var string
     */
    public $table = 'StoreCompanyProfileFood';

    /**
     * 普通商品
     * @return array|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $this->title = '视频管理';
        $get = $this->request->get();
        $db = Db::name($this->table)->where(['is_deleted' => '0']);


        /*if (isset($get['goods_title']) && $get['goods_title'] !== '') {
            $db->whereLike('goods_title', "%{$get['goods_title']}%");
        }
        foreach (['cate_id', 'brand_id'] as $field) {
            (isset($get[$field]) && $get[$field] !== '') && $db->where($field, $get[$field]);
        }*/
        if (isset($get['create_at']) && $get['create_at'] !== '') {
            list($company_profile_food, $end) = explode(' - ', $get['create_at']);
            $db->whereBetween('create_at', ["{$company_profile_food} 00:00:00", "{$end} 23:59:59"]);
            $db->whereBetween('create_at', ["{$company_profile_food} 00:00:00", "{$end} 23:59:59"]);
        }

        return parent::_list($db->order('status desc,id desc'));
    }

    /**
     * 商城数据处理
     * @param array $data
     */
    protected function _data_filter(&$data)
    {
        $result = CompanyProfileFoodService::buildCompanyProfileFoodList($data);

    }

    /**
     *
     *
     * 添加商品
     * @return array|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\Exception
     */
    public function add()
    {
        if ($this->request->isGet()) {
            $this->title = '添加视频';
            $this->_form_assign();
            return $this->_form($this->table, 'form');
        }
        try {
            $data = $this->_form_build_data();
                $res = Db::name($this->table)->insertGetId($data['main']);
                if(!$res){
                    $this->error('视频添加失败，请稍候再试！');
                }
        } catch (HttpResponseException $exception) {
            return $exception->getResponse();
        } catch (\Exception $e) {
            $this->error('视频添加失败，请稍候再试！');
        }
        list($base, $spm, $url) = [url('@admin'), $this->request->get('spm'), url('store/company_profile_food/index')];
        $this->success('添加视频成功！', "{$base}#{$url}?spm={$spm}");
    }

    /**
     * 编辑商品
     * @return array|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        if (!$this->request->isPost()) {
            $id = $this->request->get('id');
            $company = Db::name($this->table)->where(['id' => $id, 'is_deleted' => '0'])->find();
            empty($company) && $this->error('需要编辑的视频不存在！');
            $this->_form_assign();
            $company['goods_video'] = $company['video'];
            return $this->fetch('form', ['vo' => $company, 'title' => '编辑视频']);
        }
        try {



            $data = $this->_form_build_data();
            $id = $this->request->post('id');
            $company = Db::name($this->table)->where(['id' => $id, 'is_deleted' => '0'])->find();
            empty($company) && $this->error('视频编辑失败，请稍候再试！');

                $where = ['id' => $id, 'is_deleted' => '0'];
                Db::name('StoreCompanyProfileFood')->where($where)->update($data['main']);


        } catch (HttpResponseException $exception) {
            return $exception->getResponse();
        } catch (\Exception $e) {
            $this->error('视频编辑失败，请稍候再试！' . $e->getMessage());
        }
        list($base, $spm, $url) = [url('@admin'), $this->request->get('spm'), url('store/company_profile_food/index')];
        $this->success('视频编辑成功！', "{$base}#{$url}?spm={$spm}");
    }

    /**
     * 表单数据处理
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _form_assign()
    {
        list($where) = [[ 'is_deleted' => '0'], 'sort asc,id desc'];

        // 所有的帖子信息
        $where = [ 'status' => '1'];
        $topBesListField = 'video';
        $top = Db::name('StoreCompanyProfileFood')->field($topBesListField)->where($where)->select();

        $this->assign([

            'all'    => $top,
        ]);
    }

    /**
     * 读取POST表单数据
     * @return array
     */
    protected function _form_build_data()
    {
//        $main['title'] = $this->request->post('title', '0');
        $main['video'] = $this->request->post('video', '');
//        $main['content'] = $this->request->post('content', '0');

        return ['main' => $main];
    }

    /**
     * 删除商品
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del()
    {
        if (DataService::update($this->table)) {
            $this->success("视频删除成功！", '');
        }
        $this->error("视频删除失败，请稍候再试！");
    }

    /**
     * 禁用帖子
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        if (DataService::update($this->table)) {
            $this->success("禁用视频成功！", '');
        }
        $this->error("禁用视频失败，请稍候再试！");
    }

    /**
     * 商品禁用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        if (DataService::update($this->table)) {
            $this->success("启用视频成功！", '');
        }
        $this->error("启用视频失败，请稍候再试！");
    }

}
